package com.blackink.controller;

import com.blackink.pojo.User;
import com.blackink.service.DeptService;
import com.blackink.service.RoleService;
import com.blackink.service.UserService;
import com.blackink.tool.BaseController;
import com.blackink.tool.CustomResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    DeptService deptService;


    @RequestMapping(value = "/management")
    public String userManagement(){
        return "user/usermanagement";
    }

    @ResponseBody
    @RequestMapping(value = "/List",method = RequestMethod.GET)
    public CustomResult userList(String userName, String phone, String start, String end,Integer pageNumber, Integer pageSize){
        return success(paging(userService.queryUserList(userName,phone,start,end),pageNumber,pageSize));
    }

    @RequestMapping(value = "/add")
    public String userAdd(){
        return "user/add";
    }

    @ResponseBody
    @RequestMapping(value = "/roleList")
    public CustomResult roleList(){
        Map map = new HashMap();

        map.put("role",roleService.queryRoleList());
        map.put("dept",deptService.queryDeptList());
        return success(map);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    public CustomResult insert(String userName,String loginName,String password,String phone,
                               Integer sex,String email,Integer dept,String remark,Integer roleId){
        User user = new User();
        user.setUserName(userName);
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setPhone(phone);
        user.setSex(sex);
        user.setEmail(email);
        user.setDept(dept);
        user.setRemark(remark);
        try{
            Integer[] i =userService.addUser(user,roleId);
            if (i[0] == -1){
                return error("账号已存在");
            }else if(i[1]>0 && i[2]>0) {
                return success("操作成功");
            }else {
                return error("操作失败");
            }
        }catch (Exception e){
            return error("操作失败");
        }

    }
}
